<?
/**
 * @id $Id: goodsManager.php 10 2007-06-04 14:22:46Z gamezoom $
 * @author $Author: gamezoom $
 * @version $Revision: 10 $
 * @date $Date: 2007-06-04 14:22:46 +0000 (Mon, 04 Jun 2007) $
 * @svn $HeadURL: http://jxcfortaobao.googlecode.com/svn/trunk/core/www/goodsManager.php $
 */

L2P::using('inc.abstractWwwModule');

class goodsManager extends abstractWwwModule
{
	function defaultEvent()
	{
		return $this->showManagerForm();
	}

	function showManagerForm()
	{
		self::needLogined();

		return self::display("goodsManagerForm");
	}

	function showEditForm()
	{
		self::needLogined();

		if(isset($_GET["id"]) && $_GET["id"] > 0){
			$goods = pdoFactory::fetch("SELECT * FROM goods WHERE id=:id", array(":id" => $_GET["id"]));
			self::assign("goods", $goods);
		}else{
			self::assign("goods", $_POST + $_GET);
		}
		return self::display("goodsEditForm");
	}

	function save()
	{
		self::needLogined();

		$id = isset($_POST["id"]) ? $_POST["id"] + 0 : 0;
		$g_no = isset($_POST["g_no"]) ? trim($_POST["g_no"]) : "";
		$g_name = isset($_POST["g_name"]) ? trim($_POST["g_name"]) : "";
		$cateid = isset($_POST["cateid"]) ? $_POST["cateid"] + 0 : 0;
		$g_remind = isset($_POST["g_remind"]) ? $_POST["g_remind"] + 0 : 0;

		$errors = array();

		$len = strlen($g_no);
		if($len < 1 || $len > 10){
			$errors["g_no"] = "goods no. is error";
		}else{
			$goods = pdoFactory::fetch("SELECT * FROM goods WHERE g_no = :g_no", array(":g_no" => $g_no));
			if($goods && $goods["id"] != $id){
				$errors["g_no"] = "goods no. is exists";
			}
		}
		$len = strlen($g_name);
		if($len < 1 || $len > 100){
			$errors["g_name"] = "goods name is error";
		}else{
			$goods = pdoFactory::fetch("SELECT * FROM goods WHERE g_name = :g_name", array(":g_name" => $g_name));
			if($goods && $goods["id"] != $id){
				$errors["g_no"] = "goods name is exists";
			}
		}
		if($cateid < 1){
			$errors["cateid"] = "goods category is error";
		}else{
			$gc = pdoFactory::fetch("SELECT * FROM goods_cate WHERE id={$cateid}");
			if(!$gc){
				$errors["cateid"] = "goods category is unknown";
			}
		}

		if(count($errors) == 0){
			//create goods
			if($id == 0){
				pdoFactory::exec("INSERT INTO goods(g_name, g_no, cateid, g_remind) VALUES(:g_name, :g_no, :cateid, :g_remind)", array(":g_name" => $g_name, ":g_no" => $g_no, ":cateid" => $cateid, ":g_remind" => $g_remind));
				$last_insert_id_data = pdoFactory::fetch("SELECT LAST_INSERT_ID() AS id");
				$last_insert_id = $last_insert_id_data["id"];
				$this->logEvent("goodsManager", "create goods :\t{$last_insert_id}");
			}else{
				pdoFactory::exec("UPDATE goods SET g_name=:g_name, g_no=:g_no, cateid=:cateid, g_remind=:g_remind WHERE id={$id}", array(":g_name" => $g_name, ":g_no" => $g_no, ":cateid" => $cateid, ":g_remind" => $g_remind));
				$this->logEvent("goodsManager", "modify goods :\t{$id}");
			}

			header("Location: ?module=goodsManager");
			exit();
		}

		self::assign("errors", $errors);
		return $this->showEditForm();
	}

	function load()
	{
		self::needLogined();

		$goods_data = file_get_contents("goods.txt");
		$goods_data_lines = explode("\r\n", $goods_data);
		foreach($goods_data_lines as $line){
			$goods_data_fields = explode("\t", $line);
			
			$g_no = $goods_data_fields[0];
			$g_name = $goods_data_fields[1];
			$g_remind = $goods_data_fields[2] + 0;

			if(strlen($g_no) > 0){
				$g_no_2 = substr($g_no, 0, 2);

				$gc = pdoFactory::fetch("SELECT * FROM goods_cate WHERE name LIKE '%{$g_no_2}%' LIMIT 1");
				if($gc){
					$cateid = $gc["id"];
					$sql = "INSERT INTO goods(g_no, g_name, g_remind, cateid) VALUES('{$g_no}', '{$g_name}', '{$g_remind}', {$cateid})";
					echo $sql . ";<br />";
				}else{
					echo $line . "<br />";
				}
			}
		}
	}
}
?>